import React, { Component } from 'react'
import logo from './logo512.png'
import { Link } from 'react-router-dom'
import { Button, Input, Form, Checkbox, message } from 'antd'
import './login.less'
import { UserOutlined, LockOutlined } from '@ant-design/icons'
import { login } from '../../api/user'
import { connect } from 'react-redux'

class Login extends Component {
  render() {
    return (
      <div className="container1">
        <div className="loginBox">
          <div className="img">
            <img src={logo} alt="" />
          </div>
          <Form
            name="normal_login"
            className="login-form"
            onFinish={this.onFinish}
            initialValues={{
              remember: true,
              account: 'xiaowang',
              password: '123456',
            }}
          >
            <Form.Item
              name="account"
              rules={[
                {
                  required: true,
                  message: '请输入用户名!',
                },
              ]}
            >
              <Input
                prefix={<UserOutlined className="site-form-item-icon" />}
                placeholder="用户名"
              />
            </Form.Item>
            <Form.Item
              name="password"
              rules={[
                {
                  required: true,
                  message: '请输入密码!',
                },
              ]}
            >
              <Input
                prefix={<LockOutlined className="site-form-item-icon" />}
                type="password"
                placeholder="密码"
              />
            </Form.Item>
            <Form.Item>
              <Form.Item name="remember" valuePropName="checked" noStyle>
                <Checkbox>记住我</Checkbox>
              </Form.Item>

              <a className="login-form-forgot" href="">
                忘记密码
              </a>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                className="login-form-button"
              >
                登录
              </Button>
              或者 <Link to={'/register'}>立即注册!</Link>
            </Form.Item>
          </Form>
        </div>
      </div>
    )
  }
  onFinish = (values) => {
    // this.props.dispatch({
    //   type: 'loginSaga',
    //   payload: values,
    // })
    // this.props.history.push('/home')
    login(values).then((res) => {
      if (!res.data.code) return message.error(res.data.msg)
      let { userInfo, token } = res.data.data
      localStorage.token = token
      localStorage.userInfo = JSON.stringify(userInfo)
      this.props.history.push('/home')
      // console.log(token, userInfo)
    })
  }
  componentDidMount() {
    console.log(this.props)
  }
}

const mapStateToProps = (state) => ({})

export default connect(mapStateToProps)(Login)
